package com.cskaoyan.service;

import com.cskaoyan.bean.MarketIssue;
import com.cskaoyan.bean.MarketIssueExample;
import com.cskaoyan.bean.MarketOrderExample;
import com.cskaoyan.bean.bo.AdminIssueCreateBO;
import com.cskaoyan.bean.bo.AdminIssueListBO;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketIssueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * \* User: 余雨健
 * \* Date: 2022/10/13
 * \* Time: 17:32
 **/
@Service
public class AdminIssueServiceImpl implements AdminIssueService{

    @Autowired
    MarketIssueMapper marketIssueMapper;

    @Override
    public CommonData<MarketIssue> selectIssueList(AdminIssueListBO adminIssueListBO) {
        Integer page = adminIssueListBO.getPage();
        Integer limit = adminIssueListBO.getLimit();
        MarketIssueExample marketIssueExample = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = marketIssueExample.createCriteria();
        if(adminIssueListBO.getQuestion() != null){
            criteria.andQuestionLike("%" + adminIssueListBO.getQuestion() + "%");
        }
        List<MarketIssue> marketIssues = marketIssueMapper.selectByExample(marketIssueExample);
        int total = marketIssues.size();
        int papes = (total + limit - 1) / limit;
        return new CommonData<MarketIssue>(total,papes,limit,page,marketIssues);
    }

    @Override
    public MarketIssue createIssue(AdminIssueCreateBO adminIssueCreateBO) {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Date = formatter.format(date);
        MarketIssue marketIssue = new MarketIssue();
        try {
            java.util.Date date1 = formatter.parse(Date);
            marketIssue.setAnswer(adminIssueCreateBO.getAnswer());
            marketIssue.setQuestion(adminIssueCreateBO.getQuestion());
            marketIssue.setAddTime(date1);
            marketIssue.setUpdateTime(date1);
            marketIssueMapper.insertSelective(marketIssue);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return marketIssue;
    }

    @Override
    public void updateIssue(MarketIssue marketIssue) {
        marketIssueMapper.updateByPrimaryKeySelective(marketIssue);
    }

    @Override
    public void deleteIssue(MarketIssue marketIssue) {
        Integer id = marketIssue.getId();
        marketIssueMapper.deleteByPrimaryKey(id);
    }
}
